//方向代理手动实现

package main

import (
	"bufio"
	"net/http"
	"net/url"
)

var proxy_addr = "http://127.0.0.1:8000"

func main() {
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		//1、 解析目标机器的地址，并更改实际请求体的协议和主机为目标
		proxy, _ := url.Parse(proxy_addr)
		r.URL.Scheme = proxy.Scheme
		r.URL.Host = proxy.Host

		//2、请求代理的目标机器
		transport := http.DefaultTransport
		response, _ := transport.RoundTrip(r)

		//3、将目标机器的返回值，返回给客户端
		for k, vv := range response.Header {
			for _, v := range vv {
				w.Header().Add(k, v)
			}
		}
		defer response.Body.Close()
		bufio.NewReader(response.Body).WriteTo(w)

	})

	http.ListenAndServe(":8002", nil)

}
